type WindowContext = {
	document: Document;
	HTMLElement: typeof HTMLElement;
	SVGElement: typeof SVGElement;
	MathMLElement: typeof MathMLElement;
	Node: typeof Node;
	customElements: CustomElementRegistry;
};

export let context: WindowContext = typeof window !== 'undefined' ? window : globalThis;

export function noWindowFound(): never {
	throw new Error('no window found');
}

export function useContext<R>(ctx: WindowContext, proc: () => R) {
	const previousContext = context;
	context = ctx;
	try {
		return proc();
	} finally {
		context = previousContext;
	}
}
